#pragma once

#include <string>
#include <vector>
#include <memory>

#include "client.h"
#include "common/seq.h"


namespace evm { namespace compute { namespace network {

using namespace std;

struct NetworkRequest {
    string port_id;
    string network_id;
};

using NetworkRequestPtr = shared_ptr<NetworkRequest>;
using NetworkRequests = vector<NetworkRequestPtr>;
using NetworkRequestsPtr = shared_ptr<NetworkRequests>;

struct API {
    bool allocate_for_instance(InstancePtr instance, string vpn, NetworkRequestsPtr requested_networks,
                               shared_ptr<vector<string>> macs, string security_groups, string dhcp_options)
    {
        auto client = Client();

        for (auto& request: *requested_networks) {
            // Process a request to use a pre-existing port.
            if (request->port_id) {
                // Make sure the port exists.
                Json port = client.show_port(request->port_id)["port"];

                // Make sure the port isn't already attached to another instance.
                if (!evm::get<string>(port, "device_id").empty())
                    return false;

                // Make sure the port is usable
                if (evm::get<string>(port, "binding:vif_type") == "binding_failed")
                    return false;

                if (macs) {
                    if (evm::find(macs, port["mac_address"].get<string>()))
                        return false;

                }

                // If requesting a specific port, automatically process
                // the network for that port as if it were explicitly requested.
                request->network_id = port["network_id"].get<string>();
            }

            // Process a request to use a specific network.

        }
    }

    void deallocate_for_instance(InstancePtr instance, NetworkRequestsPtr requested_networks = nullptr)
    {

    }

    void process_requested_networks(NetworkRequestsPtr requested_networks) {

    }
};

}}}
